﻿using CgdataBase;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using MessageBox = FolderCompare.Views.MessageBox;

namespace FolderCompare.ViewModels
{
    public class WinTextCompareViewModel : BindableBase
    {
        private AppSettings Settings => AppSettings.Instance;
        public int FontSize => Settings.FontSize;
        public string FontName => Settings.FontName;
        public string Title => "文本比较";

        public string OldText { get; set; }
        public string NewText { get; set; }

        public DelegateCommand SubmitCommand { get; }

        public WinTextCompareViewModel(string oldFile, string newFile)
        {
            OldText = ReadAllText(oldFile);
            NewText = ReadAllText(newFile);
            SubmitCommand = new DelegateCommand(OnSubmit);
        }

        private string ReadAllText(string fileName)
        {
            Encoding encoding = TextCodeGuessHelper.GuessFileEncoding(fileName);
            if (encoding == null)
            {
                return File.ReadAllText(fileName);
            }
            else
            {
                return File.ReadAllText(fileName, encoding);
            }
        }

        private void OnSubmit()
        {
            App.GetActiveWindow().DialogResult = true;
        }
    }
}